﻿// Write a program that finds the most frequent number in an array. Example:
//	{4, 1, 1, 4, 2, 3, 4, 4, 1, 2, 4, 9, 3} -> 4 (5 times)

using System;

class FindMostFrequentNumber
{
    static void Main()
    {
        // get the length of the array
        Console.Write("Enter length of the array : ");
        string length = Console.ReadLine();
        int lengthOfArray;
        while (!int.TryParse(length, out lengthOfArray))
        {
            Console.Write("Invalid input. Enter again length for the array : ");
            length = Console.ReadLine();
        }
        if (lengthOfArray < 0)
        {
            Console.WriteLine("Invalid input. The length should be bigger than 0. Start the program again.");
        }
        else
        {

            decimal[] array = new decimal[lengthOfArray];

            for (int index = 0; index < lengthOfArray; index++)
            {
                // get the element of the array
                Console.Write("Enter element of the array : ");
                string element = Console.ReadLine();
                decimal elementOfArray;
                while (!decimal.TryParse(element, out elementOfArray))
                {
                    Console.Write("Invalid input. Enter element again : ");
                    element = Console.ReadLine();
                }
                array[index] = elementOfArray;
            }

            decimal mostFrequentNumber = 0;
            decimal temporaryNumber = 0;
            int countMostFrequent = 0;
            int temporaryCount = 0;

            for (int index = 0; index < lengthOfArray; index++)
            {
                if (array[index] != decimal.MinValue)
                {


                    temporaryNumber = array[index];
                    temporaryCount++;
                    for (int i = index + 1; i < lengthOfArray; i++)
                    {
                        if (array[i] == temporaryNumber)
                        {
                            temporaryCount++;
                        }
                    }
                    if (temporaryCount > countMostFrequent)
                    {
                        countMostFrequent = temporaryCount;
                        mostFrequentNumber = array[index];
                    }
                    temporaryCount = 0;
                }
            }
            Console.WriteLine("The number that is met most frequently is : " + mostFrequentNumber + "(" + countMostFrequent + " times)");
        }
    }
}
